<!-- Home -->
<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center" class="center">购物街</div>
    </nav-bar>
    <scroll class="content" ref="scroll" :probe-type="3" @scroll="contentScroll" @pullingUp="loadMore">
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"/>
      <recommend-view :recommends="recommends" />
      <feature-view />
      <tab-control class="tab-control" :titles='["流行","精选","新款"]' @tabClick="tabClick"></tab-control>
      <goods-list :goods="showGoodsList" />
    </scroll>
    <back-top class="back-top" @click.native="backTopClick" v-show="isShowBackTop" />
  </div>
</template>

<script>
  import HomeSwiper from "./HomeComps/HomeSwiper";
  import RecommendView from "./HomeComps/RecommendView";
  import FeatureView from "./HomeComps/FeatureView";

  import NavBar from "components/common/navbar/NavBar";
  import TabControl from "components/content/tabControl/TabControl";
  import GoodsList from "components/content/goods/GoodsList";
  import Scroll from "components/common/scroll/Scroll";
  import BackTop from "components/content/backTop/BackTop";
  import {
    getHomeMutidata,
    getHomeGoods
  } from "network/home";

  export default {
    name: "Home",
    components: {
      HomeSwiper,
      RecommendView,
      FeatureView,

      NavBar,
      TabControl,
      GoodsList,
      Scroll,
      BackTop

    },
    data() {
      return {
        banners: [],
        recommends: [],
        titles: [],
        goods: {
          "pop": {
            page: 0,
            list: []
          },
          "sell": {
            page: 0,
            list: []
          },
          "new": {
            page: 0,
            list: []
          },
        },
        currentType: 'pop',
        isShowBackTop: true,
        args: ""
      };
    },
    //生命周期 - 创建完成（访问当前this实例）
    created() {
      this.getHomeMutidata();
      this.getHomeGoods('pop');
      this.getHomeGoods('new');
      this.getHomeGoods('sell');

    },
    mounted() {
      const refresh = this.debounce(this.$refs.scroll.refresh, 50);
      this.$bus.$on("itemImageLoad", () => {
        refresh();
      })

      // this.$bus.$on("itemImageLoad",()=>{
      //   this.$refs.scroll.refresh()
      //   console.log("_____")
      // }) 
    },
    //生命周期 - 挂载完成（访问DOM元素）
    computed: {
      showGoodsList() {
        return this.goods[this.currentType].list;
      }
    },
    methods: {

      /**
       * 事件监听相关数据
       */
      debounce(func, delay) {
        let timer = null;
        return function (...args) {
          if (timer) clearTimeout(timer)
          timer = setTimeout(() => {
            func.apply(this, args)
          }, delay)
        }
      },
      tabClick(index) {
        console.log(index)
        switch (index) {
          case 0:
            this.currentType = 'pop';
            break;
          case 1:
            this.currentType = 'sell';
            break;
          case 2:
            this.currentType = 'new';
            break;

        }
      },
      swiperImageLoad(){
        console.log(this.$refs.tabControl.$el.offsetTop)
      },

      /**
       * 请求一些首页数据
       */
      getHomeMutidata() {
        getHomeMutidata().then(res => {
          this.banners = res.data.banner.list;
          this.recommends = res.data.recommend.list;
        });
      },
      /**
       * 请求商品数据
       */
      getHomeGoods(type) {
        const page = this.goods[type].page + 1;
        getHomeGoods(type, page).then(res => {
          this.goods[type].list.push(...res.data.list);
          this.goods[type].page += 1;
          this.$refs.scroll.finishPullUp();
        })
      },
      backTopClick() {
        this.$refs.scroll.scrollTo(0, 0)
      },
      contentScroll(position) {
        this.isShowBackTop = (-position.y) > 1000;
      },
      loadMore() {
        console.log("上拉加载更多")
        this.getHomeGoods(this.currentType)
      }
    },
  };

</script>
<style scoped>
  /* @import url(); 引入css类 */
  #home {
    padding-top: 44px;
    height: 100vh;
  }

  .home-nav {
    background-color: var(--color-tint);
    font-weight: 700;
    color: #fff;

    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 9;
  }

  .center {
    text-align: center;
    color: white;
  }

  .tab-control {
    position: sticky;
    top: 44px;
    z-index: 10
  }

  .content {
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }

  .fixed {
    position: fixed;
    top: 44px;
    left: 0;
    right: 0;
  }

  .back-top {
    position: fixed;
    right: 10px;
    bottom: 60px;
  }

</style>
